package org.springblade.core.mp.support;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Condition {

    public static <T> IPage<T> getPage(Query query) {
        Page<T> page = new Page((long) Func.toInt(query.getCurrent(), 1), (long)Func.toInt(query.getSize(), 10));

        if (Func.isNotBlank(query.getOrders())) {
            String[] order = Func.toStrArray(query.getOrders());

            for (String s : order) {
                String[] strArray = Func.toStrArray(":", s);
                String var = StringUtil.humpToUnderline(strArray[0]);
                String type = strArray[1];

                if (type.equals("ascend")) {
                    page.addOrder(new OrderItem[]{OrderItem.asc(StringUtil.cleanIdentifier(var))});
                } else if (type.equals("descend")) {
                    page.addOrder(new OrderItem[]{OrderItem.desc(StringUtil.cleanIdentifier(var))});
                }
            }
        } else {
            String[] ascArr = Func.toStrArray(query.getAscs());
            String[] descArr = ascArr;
            int var4 = ascArr.length;

            int var5;
            for(var5 = 0; var5 < var4; ++var5) {
                String asc = descArr[var5];
                page.addOrder(new OrderItem[]{OrderItem.asc(StringUtil.cleanIdentifier(asc))});
            }

            descArr = Func.toStrArray(query.getDescs());
            String[] var8 = descArr;
            var5 = descArr.length;

            for(int var9 = 0; var9 < var5; ++var9) {
                String desc = var8[var9];
                page.addOrder(new OrderItem[]{OrderItem.desc(StringUtil.cleanIdentifier(desc))});
            }
        }

        return page;
    }

    public static <T> QueryWrapper<T> getQueryWrapper(T entity) {
        return new QueryWrapper(entity);
    }

    public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Class<T> clazz) {
        Kv exclude = Kv.create().set("Blade-Auth", "Blade-Auth").set("current", "current").set("size", "size").set("ascs", "ascs").set("descs", "descs");
        return getQueryWrapper(query, exclude, clazz);
    }

    public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Map<String, Object> exclude, Class<T> clazz) {
        exclude.forEach((k, v) -> {
            query.remove(k);
        });
        QueryWrapper<T> qw = new QueryWrapper();
        qw.setEntity(BeanUtil.newInstance(clazz));
        SqlKeyword.buildCondition(query, qw);
        return qw;
    }

    public static <T> QueryWrapper<T> addOrder(QueryWrapper<T> queryWrapper, Query query) {
        List<OrderItem> order = Condition.getOrder(query);
        return addOrder(queryWrapper, order);
    }

    public static <T> LambdaQueryWrapper<T> addOrder(LambdaQueryWrapper<T> queryWrapper, Query query) {
        List<OrderItem> order = Condition.getOrder(query);
        return addOrder(queryWrapper, order);
    }

    public static <T> QueryWrapper<T> addOrder(QueryWrapper<T> query, List<OrderItem> items) {
        for (OrderItem item : items) {
            query.orderBy(true, item.isAsc(), item.getColumn());
        }
        return query;
    }

    public static <T> LambdaQueryWrapper<T> addOrder(LambdaQueryWrapper<T> query, List<OrderItem> items) {
        if (Func.isNotEmpty(items)) {
            StringBuffer sb = new StringBuffer();
            if (query.getTargetSql().contains("ORDER BY")) {
                sb.append(", ");
            } else {
                sb.append("order by ");
            }

            for (OrderItem item : items) {
                String column = item.getColumn();
                sb.append(column);
                sb.append(item.isAsc() ? " asc" : " desc");
                sb.append(", ");
            }

            sb.delete(sb.length() - 2, sb.length() - 1);

            query.last(sb.toString());
        }

        return query;
    }

    public static List<OrderItem> getOrder(Query query) {
        List<OrderItem> res = new ArrayList<>();
        if (query == null) {
            return res;
        }
        String[] order = Func.toStrArray(query.getOrders());

        for (String s : order) {
            String[] strArray = Func.toStrArray(":", s);
            String var = StringUtil.humpToUnderline(strArray[0]);
            String type = strArray[1];

            if (type.equals("ascend")) {
                res.add(OrderItem.asc(StringUtil.cleanIdentifier(var)));
            } else if (type.equals("descend")) {
                res.add(OrderItem.desc(StringUtil.cleanIdentifier(var)));
            }
        }

        return res;
    }

    public static <T> IPage<T> getPage(IPage page, List<T> ls) {
        Page<T> tPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        tPage.setRecords(ls);
        return tPage;
    }
}
